package cn.yanlin.springcloud.controller;

import cn.yanlin.springcloud.entities.CommonResult;
import cn.yanlin.springcloud.entities.Payment;
import cn.yanlin.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhangyanlin
 * @version 1.0
 * @date 2021/10/7 21:35
 */
@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;
    @Resource
    public DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverPort;
    @PostMapping(value = "/payment/create")
    public CommonResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("***插入结果***"+result);
        //soudsd
        if (result>0){
            return new CommonResult(200,"插入成功,serverPort"+serverPort,result);
        }else {
            return new CommonResult(404,"插入失败,serverPort"+serverPort,null);
        }
        // 测试：Git提交冲突测试！
        // 测试：Git提交冲突测试！
        //System.out.println("11111");
        //sou

    }
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("***查询结果***"+payment);
        if (payment!=null){
            return new CommonResult(200,"查询成功,serverPort"+serverPort,payment);
        }else {
            return new CommonResult(404,"查询失败,查询ID为:"+id,null);
        }
    }
    @GetMapping(value = "/payment/discovery")
    public Object discovery(){
        //eureka中的服务名称集合
        //格式 icloud-payment-service     icloud-order-service
        List<String> services = discoveryClient.getServices();
        for (String i : services) {
            log.info("i"+i);
        }
        //service下的实例信息集合(名称,端口号,URL)
        //格式 CLOUD-PAYMENT-SERVICE	8001	http://192.168.137.1:8001
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info(instance.getServiceId()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        System.out.println("新增了一句话");
        System.out.println("对着个方法进行了修改");
        System.out.println("000");
        return this.discoveryClient;
    }
}